package com.ycy.io.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 *
 * socket 之udp
 *   (1) 没有服务器/客户端一说, 只有发送者/接受者一说
 *   (2) 没有三次握手和四次挥手
 *   (3) 速度快, 但是会掉包
 * @Author: ycy
 * @CreateDate: 2018/11/7 12:32
 */
public class RecieveDemo {

  public static void main(String[] args) {

    try {
      DatagramSocket datagramSocket = new DatagramSocket(10001);
      byte [] B = new byte[1024];
      DatagramPacket datagramPacket = new DatagramPacket(B,0,1024);

      while(true){
        datagramSocket.receive(datagramPacket);
        String s = new String(datagramPacket.getData());
        System.out.println(s);
      }


    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
